<?php


	/*Go through one building at a time*/
	foreach ($buildings as $building)
	{
		get $subjects from $building;
		/*Schedule Graduate Level Courses First*/
		/*Go through each subect within the building*/
		foreach ($subjects as $subject)
		{
			get $graduate_level_course_sections from $subject;
			foreach ($graduate_level_course_sections as $section)
			{
				get $instructor_availability of $section.$instructor;
				$available = '';
				
				/*Create SQL constraints based on the instructor's availability*/
				foreach ($instructor_availability as $slot)
				{
					$available .= $slot;
				}
				
				/*Get all classrooms of the correct building, type, and capacity which is available during the instructor's availability/
				get $classrooms of type $section.$type and of building $building and of capacity >= $section.num_seats and of $availabile order by num_seats ascending;
				
				/*Check the availability of each classroom, one at a time. If it's available, break the loop and schedule the classroom.*/
				$schedule_classroom = NULL;
				foreach ($classrooms as $classroom)
				{
					get $scheduled_courses in $classroom with end_time > 17:30 ;
					if (num_rows of $scheduled_courses == 0)
					{
						$schedule_classroom = $classroom;
						break;
					}
				}
				
				/*If an available classroom has been found, schedule it. Otherwise, move on to the next course section.*/
				if (!empty($schedule_classroom))
				{
					schedule $section with $schedule_classroom and $timeslot;
				}
				
			}
			
			
			
			
		}
		
		
		/*Next, schedule undergrad HONOR courses while trying to not schedule honor courses in the same timeslots*/
		foreach ($subjects as $subject)
		{
			get $undergrad_level_course_sections from $subject where honors_program = 1 order by course_number descending;
			foreach ($undergrad_level_course_sections as $section)
			{
				get $instructor_availability of $section.$instructor;
				$available = '';
				
				foreach ($instructor_availability as $slot)
				{
					$available .= $slot;
				}
				
				get $timeslots where $available order by $instructor_preference descending;
				foreach($timeslots as $timeslot)
				{
					
					get $scheduled_courses of $section.$subject where honors_program = 1;
					foreach($scheduled_courses as $sc)
					{
						if (($sc.start_time < $timeslot.start_time && $sc.end_time > $timeslot.start_time) || ($sc.start_time < $timeslot.end_time && $sc.end_time > $timeslot.end_time) || ($sc.start_time <= $timeslot.start_time && $sc.end_time >= $timeslot.end_time) || ($sc.start_time >= $timeslot.start_time && $sc.end_time <= $timeslot.end_time))
						{
							skip to next $timeslot;
						}
					}
					
					get $classrooms of type $section.$type and of building $building and of capacity >= $section.num_seats order by num_seats ascending;
					
					foreach ($classrooms as $classroom)
					{
						$classroom_available = TRUE;
						get $scheduled_courses in $classroom;
						foreach ($scheduled_courses as $sc)
						{
							if (($sc.start_time < $timeslot.start_time && $sc.end_time > $timeslot.start_time) || ($sc.start_time < $timeslot.end_time && $sc.end_time > $timeslot.end_time) || ($sc.start_time <= $timeslot.start_time && $sc.end_time >= $timeslot.end_time) || ($sc.start_time >= $timeslot.start_time && $sc.end_time <= $timeslot.end_time))
							{
								$classroom_available = FALSE;
							}
						}
						
						$schedule_classroom = NULL;
						if ($classroom_available)
						{
							$schedule_classroom = $classroom;
							break;
						}
					}
					
					if (!empty($schedule_classroom))
					{
						schedule $section with $schedule_classroom and $timeslot;
					}
				}
			}
		}
		
		/*Next, schedule undergrad courses (including left over honor courses) while trying to not schedule courses of the same level in the same timeslots*/
		foreach ($subjects as $subject)
		{
			get $undergrad_level_course_sections from $subject order by course_number descending;
			foreach ($undergrad_level_course_sections as $section)
			{
				get $instructor_availability of $section.$instructor;
				$available = '';
				
				foreach ($instructor_availability as $slot)
				{
					$available .= $slot;
				}
				
				get $timeslots where $available order by $instructor_preference descending;
				foreach($timeslots as $timeslot)
				{
					
					get $scheduled_courses of $section.$subject and $section.level;
					foreach($scheduled_courses as $sc)
					{
						if (($sc.start_time < $timeslot.start_time && $sc.end_time > $timeslot.start_time) || ($sc.start_time < $timeslot.end_time && $sc.end_time > $timeslot.end_time) || ($sc.start_time <= $timeslot.start_time && $sc.end_time >= $timeslot.end_time) || ($sc.start_time >= $timeslot.start_time && $sc.end_time <= $timeslot.end_time))
						{
							skip to next $timeslot;
						}
					}
					
					get $classrooms of type $section.$type and of building $building and of capacity >= $section.num_seats order by num_seats ascending;
					
					foreach ($classrooms as $classroom)
					{
						$classroom_available = TRUE;
						get $scheduled_courses in $classroom;
						foreach ($scheduled_courses as $sc)
						{
							if (($sc.start_time < $timeslot.start_time && $sc.end_time > $timeslot.start_time) || ($sc.start_time < $timeslot.end_time && $sc.end_time > $timeslot.end_time) || ($sc.start_time <= $timeslot.start_time && $sc.end_time >= $timeslot.end_time) || ($sc.start_time >= $timeslot.start_time && $sc.end_time <= $timeslot.end_time))
							{
								$classroom_available = FALSE;
							}
						}
						
						$schedule_classroom = NULL;
						if ($classroom_available)
						{
							$schedule_classroom = $classroom;
							break;
						}
					}
					
					if (!empty($schedule_classroom))
					{
						schedule $section with $schedule_classroom and $timeslot;
					}
				}
			}
		}
		
		
		/*Next, schedule undergrad level courses without regard to courses of the same level falling in the same timeslot.*/
		foreach ($subjects as $subject)
		{
			get $undergrad_level_course_sections from $subject order by course_number descending;
			foreach ($undergrad_level_course_sections as $section)
			{
				get $instructor_availability of $section.$instructor;
				$available = '';
				
				foreach ($instructor_availability as $slot)
				{
					$available .= $slot;
				}
				
				get $timeslots where $available order by $instructor_preference descending;
				foreach($timeslots as $timeslot)
				{
					/*
					get $scheduled_courses of $section.$subject and $section.level;
					foreach($scheduled_courses as $sc)
					{
						if (($sc.start_time < $timeslot.start_time && $sc.end_time > $timeslot.start_time) || ($sc.start_time < $timeslot.end_time && $sc.end_time > $timeslot.end_time) || ($sc.start_time <= $timeslot.start_time && $sc.end_time >= $timeslot.end_time) || ($sc.start_time >= $timeslot.start_time && $sc.end_time <= $timeslot.end_time))
						{
							skip to next $timeslot;
						}
					}*/
					
					get $classrooms of type $section.$type and of building $building and of capacity >= $section.num_seats order by num_seats ascending;
					
					foreach ($classrooms as $classroom)
					{
						$classroom_available = TRUE;
						get $scheduled_courses in $classroom;
						foreach ($scheduled_courses as $sc)
						{
							if (($sc.start_time < $timeslot.start_time && $sc.end_time > $timeslot.start_time) || ($sc.start_time < $timeslot.end_time && $sc.end_time > $timeslot.end_time) || ($sc.start_time <= $timeslot.start_time && $sc.end_time >= $timeslot.end_time) || ($sc.start_time >= $timeslot.start_time && $sc.end_time <= $timeslot.end_time))
							{
								$classroom_available = FALSE;
							}
						}
						
						$schedule_classroom = NULL;
						if ($classroom_available)
						{
							$schedule_classroom = $classroom;
							break;
						}
					}
					
					if (!empty($schedule_classroom))
					{
						schedule $section with $schedule_classroom and $timeslot;
					}
				}
			}
		}
	}
	
	
	/************************************************************************/
	/*				END OF INITIAL SCHEDULING PASS THROUGH					*/
	/************************************************************************/
	
	
	
	
	/*Go through one building at a time*/
	foreach ($buildings as $building)
	{
		get $subjects from $builing;
		foreach ($subjects as $subject)
		{
			/*Get all classes for this building that are not yet scheduled*/
			get $undergrad_level_course_sections from $subject where not in schedule order by course_number descending;
			foreach ($undergrad_level_course_sections as $section)
			{
				get $timeslots within the instructors availability of $building;
				foreach ($timeslots as $timelsot)
				{
					get $scheduled_sections in $timeslot;
					foreach($scheduled_sections as $scheduled_section)
					{
						$currTimeslot = $scheduled_section.$timeslot;
						$currClassroom = $scheduled_section.$classroom;
						get $instructor_availability of $scheduled_section.$instructor;
						$available = '';
						
						foreach ($instructor_availability as $slot)
						{
							$available .= $slot;
						}
						
						get $timeslots where $available order by $instructor_preference descending;
						foreach($timeslots as $new_timeslot)
						{
							/*
							get $scheduled_courses of $scheduled_section.$subject and $scheduled_section.level;
							foreach($scheduled_courses as $sc)
							{
								if (($sc.start_time < $timeslot.start_time && $sc.end_time > $timeslot.start_time) || ($sc.start_time < $timeslot.end_time && $sc.end_time > $timeslot.end_time) || ($sc.start_time <= $timeslot.start_time && $sc.end_time >= $timeslot.end_time) || ($sc.start_time >= $timeslot.start_time && $sc.end_time <= $timeslot.end_time))
								{
									skip to next $timeslot;
								}
							}*/
							
							get $classrooms of type $scheduled_section.$type and of building $building and of capacity >= $scheduled_section.num_seats order by num_seats ascending;
							
							foreach ($classrooms as $classroom)
							{
								$classroom_available = TRUE;
								get $scheduled_courses in $classroom;
								foreach ($scheduled_courses as $sc)
								{
									if (($sc.start_time < $timeslot.start_time && $sc.end_time > $timeslot.start_time) || ($sc.start_time < $timeslot.end_time && $sc.end_time > $timeslot.end_time) || ($sc.start_time <= $timeslot.start_time && $sc.end_time >= $timeslot.end_time) || ($sc.start_time >= $timeslot.start_time && $sc.end_time <= $timeslot.end_time))
									{
										$classroom_available = FALSE;
									}
								}
								
								$schedule_classroom = NULL;
								if ($classroom_available)
								{
									$schedule_classroom = $classroom;
									break;
								}
							}
							
							if (!empty($schedule_classroom))
							{
								update $scheduled_section to $schedule_classroom and $new_timeslot;
								schedule $section with $currClassroom and $currTimeslot;
							}
						}
					}
				}
			}
		}
	}
	
?>